package com.ruoyi.business.iService.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ruoyi.business.domain.entity.TbStore;
import com.ruoyi.business.iService.ITbStoreService;
import com.ruoyi.business.mapper.TbStoreMapper;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 店铺Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-26
 */
@Service
public class TbStoreServiceImpl extends ServiceImpl<TbStoreMapper, TbStore> implements ITbStoreService {

    /**
     * 查询店铺列表
     *
     * @param tbStore 店铺
     * @return 店铺
     */
    @Override
    public List<TbStore> selectTbStoreList(TbStore tbStore) {
        QueryWrapper queryWrapper = buildQuery(tbStore);
        return mapper.selectListByQuery(queryWrapper);
    }

    /**
     * 分页查询店铺列表
     *
     * @param tbStore 店铺
     * @return 店铺
     */
    @Override
    public TableDataInfo selectTbStorePage(TbStore tbStore, PageDomain pageDomain) {
        QueryWrapper queryWrapper = buildQuery(tbStore);
        Page<TbStore> page = super.page(new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize()), queryWrapper);
        return TableDataInfo.build(page);
    }

    private QueryWrapper buildQuery(TbStore bo) {
        QueryWrapper queryWrapper = query();
        // queryWrapper.orderByAsc(TbStore::getStoreId);
        queryWrapper.eq(TbStore::getUserId, bo.getUserId(), bo.getUserId() != null);
        queryWrapper.like(TbStore::getStoreName, bo.getStoreName(), StringUtils.isNotBlank(bo.getStoreName()));
        queryWrapper.eq(TbStore::getStoreLogo, bo.getStoreLogo(), StringUtils.isNotBlank(bo.getStoreLogo()));
        queryWrapper.eq(TbStore::getStoreType, bo.getStoreType(), bo.getStoreType() != null);
        queryWrapper.eq(TbStore::getStoreStatus, bo.getStoreStatus(), bo.getStoreStatus() != null);
        queryWrapper.eq(TbStore::getContactsOwner, bo.getContactsOwner(), StringUtils.isNotBlank(bo.getContactsOwner()));
        queryWrapper.eq(TbStore::getContactsTel, bo.getContactsTel(), StringUtils.isNotBlank(bo.getContactsTel()));
        queryWrapper.eq(TbStore::getStoreTel, bo.getStoreTel(), StringUtils.isNotBlank(bo.getStoreTel()));
        queryWrapper.eq(TbStore::getOpenTime, bo.getOpenTime(), StringUtils.isNotBlank(bo.getOpenTime()));
        queryWrapper.eq(TbStore::getProvince, bo.getProvince(), StringUtils.isNotBlank(bo.getProvince()));
        queryWrapper.eq(TbStore::getCity, bo.getCity(), StringUtils.isNotBlank(bo.getCity()));
        queryWrapper.eq(TbStore::getArea, bo.getArea(), StringUtils.isNotBlank(bo.getArea()));
        queryWrapper.eq(TbStore::getAddressDetail, bo.getAddressDetail(), StringUtils.isNotBlank(bo.getAddressDetail()));
        queryWrapper.eq(TbStore::getCoordinate, bo.getCoordinate(), StringUtils.isNotBlank(bo.getCoordinate()));
        queryWrapper.eq(TbStore::getBusinessLicense, bo.getBusinessLicense(), StringUtils.isNotBlank(bo.getBusinessLicense()));
        queryWrapper.eq(TbStore::getStratDeliveryPrice, bo.getStratDeliveryPrice(), bo.getStratDeliveryPrice() != null);
        queryWrapper.eq(TbStore::getPromoterId, bo.getPromoterId(), bo.getPromoterId() != null);
        return queryWrapper;
    }

}
